#include "basic_board.h"
#include "led_board.h"
#include "device_parameter.h"

/**
 * @brief 配置键盘指示灯管脚功能
 *
 */
static void KeyboardLedPinConfiguration(void);

/**
 * @brief 配置事件指示灯管脚功能
 *
 */
static void EventLedPinConfiguration(void);

/**
 * @brief 配置警报指示灯管脚功能
 *
 */
static void AlarmLedPinConfiguration(void);

/**
 * @brief 设置键盘指示灯为高电平
 *
 */
static void SetKeyboardLedPinHightLevel(void);

/**
 * @brief 设置键盘指示灯为低电平
 *
 */
static void SetKeyboardLedPinLowLevel(void);

/**
 * @brief 设置事件指示灯为高电平
 *
 */
static void SetEventLedPinHightLevel(void);

/**
 * @brief 设置事件指示灯为低电平
 *
 */
static void SetEventLedPinLowLevel(void);

/**
 * @brief 设置事件指示灯为低电平
 *
 */
static void SetAlarmLedPinHightLevel(void);

/**
 * @brief 设置警报指示灯为高电平
 *
 */
static void SetAlarmLedPinHightLevel(void);

/**
 * @brief 设置警报指示灯为低电平
 *
 */
static void SetAlarmLedPinLowLevel(void);

/**
 * @brief 设置键盘指示灯管脚电平
 *
 * @param PinLevel - 管脚电平（true：高电平 false：低电平）
 */
static void SetKeyboardLedPinLevel(bool PinLevel);

/**
 * @brief 恢复其他指示灯状态
 * 
 */
static void RestoreOtherLedStatus(void);

/**
 * @brief 设置警报指示灯管脚电平
 *
 * @param PinLevel - 管脚电平（true：高电平 false：低电平）
 */
static void SetAlarmLedPinLevel(bool PinLevel);

/**
 * @brief 配置事件指示灯管脚功能
 *
 */
static void SetEventLedPinLevel(bool PinLevel);

/**
 * @brief 关闭警报呼吸灯
 *
 */
static void CloseAlarmBreathingLight(void);

/**
 * @brief Set the Result Led Pin High Level object
 *
 */
static void SetResultLedPinHighLevel(void);

/**
 * @brief Set the Result Led Pin Low Level object
 *
 */
static void SetResultLedPinLowLevel(void);

/**
 * @brief 配置结果指示灯管脚功能
 *
 */
static void ResultLedPinConfiguration(void);

/**
 * @brief Set the Result Led Pin Level object
 *
 * @param PinLevel
 */
static void SetResultLedPinLevel(bool PinLevel);

/**
 * @brief 更新警报呼吸灯输出占空比
 *
 * @param Cycle - 占空比
 */
static void UpdateAlarmBreathingLightDutyCycle(uint8_t Cycle);

static LedStatus_t AlarmLedStatus;      // 警报指示灯状态
static LedStatus_t EventLedStatus;      // 键盘指示灯状态
static LedStatus_t KeyboardLedStatus;   // 键盘指示灯状态
static LedStatus_t ResultLedStatus;     // 结果指示灯状态 绿灯
static BreathingLight_t BreathingLight; // 呼吸灯

static void KeyboardLedPinConfiguration(void)
{
#if _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ModeCfg(BASIC_KEYBOARD_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOA_ModeCfg(BASIC_KEYBOARD_LED_PIN, GPIO_ModeOut_PP_20mA);
#endif

  SetKeyboardLedPinLowLevel();
}

static void EventLedPinConfiguration(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ModeCfg(BASIC_EVENT_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOB_ModeCfg(BASIC_EVENT_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V32_B_BOARD_
  GPIOB_ModeCfg(BASIC_EVENT_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(BASIC_EVENT_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ModeCfg(BASIC_EVENT_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(BASIC_EVENT_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _DA01_A_BOARD_
  GPIOB_ModeCfg(BASIC_EVENT_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _CR01_BU_
  GPIOB_ModeCfg(BASIC_EVENT_LED_PIN, GPIO_ModeOut_PP_20mA); // ok
#endif

  SetEventLedPinLevel(false);
}

static void AlarmLedPinConfiguration(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOA_ModeCfg(BASIC_ALARM_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOB_ModeCfg(BASIC_ALARM_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V32_B_BOARD_
  GPIOB_ModeCfg(BASIC_ALARM_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ModeCfg(BASIC_ALARM_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ModeCfg(BASIC_ALARM_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ModeCfg(BASIC_ALARM_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _DA01_A_BOARD_
  GPIOB_ModeCfg(BASIC_ALARM_LED_PIN, GPIO_ModeOut_PP_20mA);

#elif _BOARD_TYPE_ == _CR01_BU_
  GPIOB_ModeCfg(BASIC_ALARM_LED_PIN, GPIO_ModeOut_PP_20mA); // ok
#endif

  SetAlarmLedPinLevel(false);
}

static void ResultLedPinConfiguration(void)
{
#if _BOARD_TYPE_ == _CR01_BU_
  GPIOB_ModeCfg(BASIC_RESULT_LED_PIN, GPIO_ModeOut_PP_20mA);
#endif
  SetResultLedPinLevel(false);
}

static void SetKeyboardLedPinHightLevel(void)
{
#if _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_SetBits(BASIC_KEYBOARD_LED_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOA_SetBits(BASIC_KEYBOARD_LED_PIN);
#endif
}

static void SetKeyboardLedPinLowLevel(void)
{
#if _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOB_ResetBits(BASIC_KEYBOARD_LED_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOA_ResetBits(BASIC_KEYBOARD_LED_PIN);
#endif
}

static void SetEventLedPinHightLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_SetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOB_SetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _V32_B_BOARD_
  GPIOB_SetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_SetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_SetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_SetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _DA01_A_BOARD_
  GPIOB_SetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _CR01_BU_
  GPIOB_SetBits(BASIC_EVENT_LED_PIN); // ok
#endif
}

static void SetEventLedPinLowLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOB_ResetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOB_ResetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _V32_B_BOARD_
  GPIOB_ResetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ResetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ResetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ResetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _DA01_A_BOARD_
  GPIOB_ResetBits(BASIC_EVENT_LED_PIN);

#elif _BOARD_TYPE_ == _CR01_BU_
  GPIOB_ResetBits(BASIC_EVENT_LED_PIN); // ok
#endif
}

static void SetAlarmLedPinHightLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOA_SetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOB_SetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _V32_B_BOARD_
  GPIOB_SetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_SetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_SetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_SetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _DA01_A_BOARD_
  GPIOB_SetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _CR01_BU_
  GPIOB_SetBits(BASIC_ALARM_LED_PIN); // ok
#endif
}

static void SetAlarmLedPinLowLevel(void)
{
#if _BOARD_TYPE_ == _V31_A_BOARD_
  GPIOA_ResetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _V32_A_BOARD_
  GPIOB_ResetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _V32_B_BOARD_
  GPIOB_ResetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _V33_A_BOARD_
  GPIOB_ResetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _V33_B_BOARD_
  GPIOA_ResetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _V33_C_BOARD_
  GPIOB_ResetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _DA01_A_BOARD_
  GPIOB_ResetBits(BASIC_ALARM_LED_PIN);

#elif _BOARD_TYPE_ == _CR01_BU_
  GPIOB_ResetBits(BASIC_ALARM_LED_PIN); // ok
#endif
}

static void SetResultLedPinHighLevel(void)
{
#if _BOARD_TYPE_ == _CR01_BU_
  GPIOB_SetBits(BASIC_RESULT_LED_PIN); // ok
#endif
}

static void SetResultLedPinLowLevel(void)
{
#if _BOARD_TYPE_ == _CR01_BU_
  GPIOB_ResetBits(BASIC_RESULT_LED_PIN); // ok
#endif
}
static void SetKeyboardLedPinLevel(bool PinLevel)
{
  (PinLevel == false) ? SetKeyboardLedPinHightLevel() : SetKeyboardLedPinLowLevel();
}

static void SetAlarmLedPinLevel(bool PinLevel)
{
  (PinLevel == false) ? SetAlarmLedPinHightLevel() : SetAlarmLedPinLowLevel();
}

static void SetEventLedPinLevel(bool PinLevel)
{
  (PinLevel == false) ? SetEventLedPinHightLevel() : SetEventLedPinLowLevel();
}

static void SetResultLedPinLevel(bool PinLevel)
{
  (PinLevel == false) ? SetResultLedPinHighLevel() : SetResultLedPinLowLevel();
}

static void CloseAlarmBreathingLight(void)
{
#if ((_BOARD_TYPE_ == _V33_A_BOARD_) || (_BOARD_TYPE_ == _DA01_A_BOARD_))
  GPIOPinRemap(DISABLE, RB_PIN_PWMX);
#endif

#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  PWMX_ACTOUT(BASIC_EVENT_BREATHING_LIGHT_CHANNEL, 0, High_Level, DISABLE);

#else
  PWMX_ACTOUT(BASIC_ALARM_BREATHING_LIGHT_CHANNEL, 0, High_Level, DISABLE);
#endif
}

static void UpdateAlarmBreathingLightDutyCycle(uint8_t Cycle)
{
#if ((_BOARD_TYPE_ == _V33_A_BOARD_) || (_BOARD_TYPE_ == _DA01_A_BOARD_))
  GPIOPinRemap(ENABLE, RB_PIN_PWMX);
#endif

  PWMX_CLKCfg(87);
  PWMX_CycleCfg(PWMX_Cycle_255);

  uint8_t DutyCycle = (Cycle > 100) ? 100 : Cycle;
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  PWMX_ACTOUT(BASIC_EVENT_BREATHING_LIGHT_CHANNEL, (uint8_t)((255 / 100.0) * DutyCycle), High_Level, ENABLE);

#else
  PWMX_ACTOUT(BASIC_ALARM_BREATHING_LIGHT_CHANNEL, (uint8_t)((255 / 100.0) * DutyCycle), High_Level, ENABLE);
#endif
}

void PromptDeviceStartup(void)
{
  EventLedStatus.Mode = LED_HINT_STARTUP;
  EventLedStatus.Status = LED_HINT_FLASHES;
  EventLedStatus.Count = LED_FLASH_STARTUP_COUNT;
  BasicStartEventLedTaskNoWait();
}

void PromptDoorLockedRecovered(void)
{
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
}

void PromptDoorLockedTigger(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_LOCKED_TIGGER;
  AlarmLedStatus.Count = LED_FLASH_LOCKED_IN_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

// 不在有效期
void PromptOutOfValidityPeriod(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_TIME_ERROR;
  AlarmLedStatus.Count = LED_FLASH_NOT_IN_VALID_PERIOD_COUNT;
  BasicStartAlarmLedTaskNoWait();

}

void PromptDoorLock(void)
{
  AlarmLedStatus.Count = 0;
  EventLedStatus.Mode = LED_HINT_IDLE;
  EventLedStatus.Status = LED_HINT_OFF;
  BasicStartEventLedTaskNoWait();
}

// 开锁提示：结果灯（绿灯）熄灭6s之后常亮，事件灯（蓝灯）常亮6s后熄灭
void PromptDoorUnlock(void)
{
  PRINT("[led_board]: PromptDoorUnlock\r\n");
  EventLedStatus.Mode = LED_HINT_UNLOCK;
  EventLedStatus.Status = LED_HINT_FLASHES;
  EventLedStatus.Count = 1;
  BasicStartEventLedTaskNoWait();
  if (BasicGetDeviceActivationStatus() == true)
  {
    ResultLedStatus.Mode = LED_HINT_UNLOCK;
    ResultLedStatus.Status = LED_HINT_FLASHES;
    ResultLedStatus.Count = 1;
    BasicStartResultLedTaskNoWait();
  }
}


// 权限不足
void PromptAuthenticationError(void)
{
  PRINT("[led_board]: PromptAuthenticationError\r\n");
  // 关闭
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_INSUFFICIENT_PRIVILEGES;
  AlarmLedStatus.Count = LED_FLASH_AUTHENTICATION_ERROR_COUNT;
  BasicStartAlarmLedTaskNoWait();
  if (BasicGetDeviceActivationStatus() == true)
  {
  }
  else
  {
    SetEventLedPinLevel(false);
  }
}

void PromptKeyHasBeenReplaced(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_CARD_REPLACE;
  AlarmLedStatus.Count = LED_FLASH_KEY_HAS_BEEN_REPLACED_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

// 蓝灯常亮4s后关闭，然后蓝灯以1S的速度切换闪烁
void PromptDeviceIsInitializing(void)
{
  PromptAuthenticationInstallFailure();
}

void PromptStartDeviceSelfTest(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  EventLedStatus.Status = LED_HINT_BREATHING_MODE;
  EventLedStatus.Mode = LED_HINT_DEVICE_SELF_TEST;
  BasicStartEventLedTaskNoWait();

#else

#endif
}

void PromptExitDeviceSelfTest(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  EventLedStatus.Count = 0;
  EventLedStatus.Mode = LED_HINT_IDLE;
  EventLedStatus.Status = LED_HINT_OFF;
  BasicStartEventLedTaskNoWait();

#else
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
#endif
}

void PromptDeviceSelfTestError(uint8_t ErrorType)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_DECICE_SELF_TEST_ERROR;
  AlarmLedStatus.Count = LED_FLASH_DEVICE_SELF_TEST_ERROR_COUNT + ErrorType;
  BasicStartAlarmLedTaskNoWait();
}

void PromptCardAffiliationError(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_CAED_AFFILIATION_ERROR;
  AlarmLedStatus.Count = LED_FLASH_CAED_AFFILIATION_ERROR_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptThisFunctionCardHasBeenDisabled(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_FUNCTION_CARD_DISABLED;
  AlarmLedStatus.Count = LED_FLASH_FUNCTION_CARD_DISABLED_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptFalseLockAlarmRecovered(void)
{
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
}

void PromptFalseLockTigger(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_FALSE_LOCK_TIGGER;
  AlarmLedStatus.Count = LED_FLASH_FALSE_LOCK_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptLowPower(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_LOW_POWER;
  AlarmLedStatus.Count = LED_FLASH_LOW_POWER_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptSetParameterError(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_SET_PARAMETER_ERROR;
  AlarmLedStatus.Count = LED_FLASH_SET_PARAMETER_ERROR_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptSetParameterSuccess(void)
{
  CloseAllLed();
  EventLedStatus.Status = LED_HINT_FLASHES;
  EventLedStatus.Mode = LED_HINT_SET_PARAMETER_SUCCESS;
  EventLedStatus.Count = LED_FLASH_SET_PARAMETER_SUCCESS_COUNT;
  BasicStartEventLedTaskNoWait();
}

void PromptSystemLocked(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_SYSTEM_LOCKED;
  AlarmLedStatus.Count = LED_FLASH_SYSTEM_LOCKED_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptStartLocalRegistration(void)
{
  EventLedStatus.Count = 0;
  EventLedStatus.Status = LED_HINT_NORMAILON;
  EventLedStatus.Mode = LED_HINT_START_LOCAL_REGISTER;
  BasicStartEventLedTaskNoWait();
}

void PromptExitLocalRegistration(void)
{
  EventLedStatus.Count = 0;
  EventLedStatus.Mode = LED_HINT_IDLE;
  EventLedStatus.Status = LED_HINT_OFF;
  BasicStartEventLedTaskNoWait();
}

void PromptKeyOperationError(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_KEY_OPERATION_ERROR;
  AlarmLedStatus.Count = LED_FLASH_KEY_OPERATION_ERROR_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptKeyOperationSuccess(void)
{
  EventLedStatus.Status = LED_HINT_FLASHES;
  EventLedStatus.Mode = LED_HINT_KEY_OPERATION_SUCCESS;
  AlarmLedStatus.Count = LED_FLASH_KEY_OPERATION_SUCCESS_COUNT;
  BasicStartEventLedTaskNoWait();
}

void PromptExitDeviceLockedState(void)
{
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
}

void PromptDeviceIsInLockedState(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_DEVICE_LOCKED_STATE;
  AlarmLedStatus.Count = LED_FLASH_DEVICE_LOCKED_STATUS_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptNetworkAttachmentEnd(void)
{
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
}

void PromptNetworkAttachmentStart(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  EventLedStatus.Status = LED_HINT_BREATHING_MODE;
  EventLedStatus.Mode = LED_HINT_NETWORK_ATTACHMENT_START;
  BasicStartEventLedTaskNoWait();

#else
  AlarmLedStatus.Status = LED_HINT_BREATHING_MODE;
  AlarmLedStatus.Mode = LED_HINT_NETWORK_ATTACHMENT_START;
  BasicStartAlarmLedTaskNoWait();
#endif
}

void PromptNetworkAttachmentSuccess(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  EventLedStatus.Count = 0;
  EventLedStatus.Mode = LED_HINT_IDLE;
  EventLedStatus.Status = LED_HINT_OFF;
  BasicStartEventLedTaskNoWait();

#else
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
#endif
}

void PromptNetworkAttachmentError(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  EventLedStatus.Count = 0;
  EventLedStatus.Mode = LED_HINT_IDLE;
  EventLedStatus.Status = LED_HINT_OFF;
  BasicStartEventLedTaskNoWait();
#endif

  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_NETWORK_ATTACHMENT_ERROR;
  AlarmLedStatus.Count = LED_FLASH_NETWORK_ATTACHMENT_ERROR_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptAntiPickingRecovered(void)
{
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
}

void PromptAntiPickingAlarm(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_ANTI_BREAKING_ALARM_TIGGER;
  AlarmLedStatus.Count = LED_FLASH_ANTI_BREAKING_ALARM_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptUserKeyedKeys(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  KeyboardLedStatus.Status = LED_HINT_NORMAILON;
  KeyboardLedStatus.Mode = LED_HINT_USER_KEYED_KEYS;
  BasicStartKeyboardLedTaskNoWait();
#endif

  EventLedStatus.Status = LED_HINT_NORMAILON;
  EventLedStatus.Mode = LED_HINT_USER_KEYED_KEYS;
  BasicStartEventLedTaskNoWait();
}

void PromptExitKeyEntry(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  KeyboardLedStatus.Count = 0;
  KeyboardLedStatus.Mode = LED_HINT_IDLE;
  KeyboardLedStatus.Status = LED_HINT_OFF;
  BasicStartKeyboardLedTaskNoWait();
#endif

  EventLedStatus.Count = 0;
  EventLedStatus.Mode = LED_HINT_IDLE;
  EventLedStatus.Status = LED_HINT_OFF;
  BasicStartEventLedTaskNoWait();
}

void PromptHostConnectionLose(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_HOST_CONNECT_LOSE;
  AlarmLedStatus.Count = LED_FLASH_HOST_CONNECT_LOSE_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptConnectTest(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  EventLedStatus.Status = LED_HINT_FLASHES;
  EventLedStatus.Mode = LED_HINT_CONNECT_TEST;
  EventLedStatus.Count = LED_FLASH_CONNECT_TEST_COUNT;
  BasicStartEventLedTaskNoWait();

#else
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_CONNECT_TEST;
  AlarmLedStatus.Count = LED_FLASH_CONNECT_TEST_COUNT;
  BasicStartAlarmLedTaskNoWait();
#endif
}

void PromptKeyRemove(void)
{
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
}

void PromptKeyInserted(void)
{
  AlarmLedStatus.Status = LED_HINT_FLASHES;
  AlarmLedStatus.Mode = LED_HINT_KEY_INSERTED;
  AlarmLedStatus.Count = LED_FLASH_KEY_INSERTED_COUNT;
  BasicStartAlarmLedTaskNoWait();
}

void PromptOnlineRegisterUser(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  EventLedStatus.Status = LED_HINT_BREATHING_MODE;
  EventLedStatus.Mode = LED_HINT_ONLINE_REGISTER_USER;
  BasicStartEventLedTaskNoWait();

#else
  AlarmLedStatus.Status = LED_HINT_BREATHING_MODE;
  AlarmLedStatus.Mode = LED_HINT_ONLINE_REGISTER_USER;
  BasicStartAlarmLedTaskNoWait();
#endif
}

void PromptExitOnlineRegisterUser(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  EventLedStatus.Count = 0;
  EventLedStatus.Mode = LED_HINT_IDLE;
  EventLedStatus.Status = LED_HINT_OFF;
  BasicStartEventLedTaskNoWait();

#else
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
#endif
}

void PromptExitSafetyModuleAuthentication(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  EventLedStatus.Count = 0;
  EventLedStatus.Mode = LED_HINT_IDLE;
  EventLedStatus.Status = LED_HINT_OFF;
  BasicStartEventLedTaskNoWait();

#else
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
#endif
}

void PromptStartSafetyModuleAuthentication(void)
{
#if ((_BOARD_TYPE_ == _V33_B_BOARD_) || (_BOARD_TYPE_ == _V33_C_BOARD_))
  EventLedStatus.Status = LED_HINT_BREATHING_MODE;
  EventLedStatus.Mode = LED_HINT_START_SAFETY_MODULE_AUTHENTICATE;
  BasicStartEventLedTaskNoWait();

#else
  AlarmLedStatus.Status = LED_HINT_BREATHING_MODE;
  AlarmLedStatus.Mode = LED_HINT_START_SAFETY_MODULE_AUTHENTICATE;
  BasicStartAlarmLedTaskNoWait();
#endif
}

// tag: 授权成功指示：常亮绿灯
void PromptAuthenticationInstallSuccess(void)
{
  ResultLedStatus.Status = LED_HINT_NORMAILON;
  ResultLedStatus.Mode = LED_HINT_STARTUP;
  ResultLedStatus.Count = 0;
  BasicStartResultLedTaskNoWait();
}

// tag: 未授权指示：蓝灯以1S的速度切换闪烁，不停
void PromptAuthenticationInstallFailure(void)
{
  PRINT("PromptAuthenticationInstallFailure\r\n");
  /** 设备安装授权状态为0(未授权) **/
  // 蓝灯以1S的速度切换闪烁，不停
  EventLedStatus.Status = LED_HINT_FLASHES;
  EventLedStatus.Mode = LED_HINT_DEVICE_INTO_STANDBY_MODE;
  EventLedStatus.Count = LOOP_FOREVER_COUNT;
  BasicStartEventLedTaskNoWait();
}

void PromptAuthenticationInstallFailureNoWait(void)
{
  PRINT("PromptAuthenticationInstallFailureNoWait\r\n");
  /** 设备安装授权状态为0(未授权) **/
  // 蓝灯以1S的速度切换闪烁，不停
  EventLedStatus.Status = LED_HINT_FLASHES;
  EventLedStatus.Mode = LED_HINT_DEVICE_INTO_STANDBY_MODE;
  EventLedStatus.Count = LOOP_FOREVER_COUNT;
  BasicStartEventLedTaskNoWait();
}

// tag: 板子固件更新状态指示：蓝灯快闪
void PromptIoBoardFirmwareUpdate(void)
{
  EventLedStatus.Status = LED_HINT_FLASHES;
  EventLedStatus.Mode = LED_HINT_SET_IO_BOARD_FIRMWARE_UPDATE_STATUS;
  EventLedStatus.Count = LOOP_FOREVER_COUNT;
  BasicStartEventLedTaskNoWait();
}

void PromptIoBoardFirmwareUpdateComplete(void)
{
  BasicStopEventLedTask();
}

// 待机模式
void PromptIntoStandby(void)
{

  if (BasicGetDeviceActivationStatus() == true)
  {
  }
  else
  {
    PromptAuthenticationInstallFailure();
  }
}

// 无论是开启固件更新，还是关闭固件更新，都是调用这个固件更新提示接口
void PromptIntoIoBoardUpdataFirmware(void)
{
  // 当前的正在处于ota状态
  if (GetIoBoardFirmwareExistFlag() == true)
  {
    // 所有灯光关闭
    CloseAllLed();
    // ota状态声光提示启动
    PromptIoBoardFirmwareUpdate();
  }
  else // ota状态声光关闭，同时启动设备待机灯光指示
  {
    // 所有灯光关闭
    CloseAllLed();
    PromptIoBoardFirmwareUpdateComplete();// 更新完成,更新状态为成功，更新成功板子计数+1
    if (BasicGetDeviceActivationStatus() == true)
    {
      PromptAuthenticationInstallSuccess();
    }
    else
    {
      PromptAuthenticationInstallFailure();
    }
  }
}

void PromptExitStandbyMode(void)
{
  AlarmLedStatus.Count = 0;
  AlarmLedStatus.Mode = LED_HINT_IDLE;
  AlarmLedStatus.Status = LED_HINT_OFF;
  BasicStartAlarmLedTaskNoWait();
}

/** 绿灯设置完成状态 **/
void PromptSetCompleteStatus(void)
{
  ResultLedStatus.Status = LED_HINT_FLASHES;
  ResultLedStatus.Mode = LED_HINT_SET_COMPLETE_STATUS;
  ResultLedStatus.Count = LED_FLASH_SET_COMP_STATUS_SUCCESS_COUNT;
  BasicStartResultLedTaskNoWait();
}

void AlarmLedNormalTurnOn(void)
{
  SetResultLedPinLevel(false);
  SetEventLedPinLevel(false);
  SetAlarmLedPinLevel(false);
  SetAlarmLedPinLevel(true);
}

void AlarmLedNormalTurnOff(void)
{
  SetAlarmLedPinLevel(false);
  SetResultLedPinLevel(true);
}

void StopAllLightAlerts(void)
{
  BasicStopAlarmLedTask();
  BasicStopEventLedTask();
  BasicStopKeyboardLedTask();

  memset(&EventLedStatus, 0, sizeof(LedStatus_t));
  memset(&AlarmLedStatus, 0, sizeof(LedStatus_t));
  memset(&KeyboardLedStatus, 0, sizeof(LedStatus_t));

  CloseAlarmBreathingLight();
  SetEventLedPinLevel(false);
  SetAlarmLedPinLevel(false);
  SetKeyboardLedPinLevel(false);
}

// 恢复其他状态
// 警报灯
static uint8_t TempEventLedTaskStaus = 0;
// 结果灯
static uint8_t TempResultLedTaskStaus = 0;
// 当前状态标志位

// 恢复之前的led状态(任务)
static void RestoreOtherLedStatus(void)
{
  PRINT("RestoreOtherLedStatus TempEventLedTaskStaus = %d\r\n", TempEventLedTaskStaus);
  PRINT("RestoreOtherLedStatus TempResultLedTaskStaus = %d\r\n", TempResultLedTaskStaus);

  // 蓝灯恢复
  switch (TempEventLedTaskStaus)
  {
  case LED_HINT_CONNECT_TEST: // 连接测试
  {

  }
  break;

  case LED_HINT_DEVICE_INTO_STANDBY_MODE: // 设备进入待机模式
  {
    // PromptAuthenticationInstallFailure();
    PromptAuthenticationInstallFailureNoWait();
  }
  break;

  default:
    break;
  }

  // 绿灯恢复
  switch (TempResultLedTaskStaus)
  {
  case LED_HINT_DEVICE_INTO_STANDBY_MODE:
  case LED_HINT_UNLOCK:
  {
    PromptAuthenticationInstallSuccess();
  }
  break;

  case LED_HINT_TIME_ERROR:
  {
    PromptOutOfValidityPeriod();
  }
  break;

  case LED_HINT_STARTUP:
  {
    PromptAuthenticationInstallSuccess();
  }
  break;

  default:
    break;
  }
  // 清空状态
  TempEventLedTaskStaus = 0;
  TempResultLedTaskStaus = 0;
}

// 蓝色事件灯
void EventLedTaskHandler(void)
{
  uint16_t NextWakeupTime = 0;

  switch (EventLedStatus.Status)
  {
  case LED_HINT_FLASHES:
  {
    switch (EventLedStatus.Mode)
    {
    case LED_HINT_SET_PARAMETER_SUCCESS:
      NextWakeupTime = ((EventLedStatus.Count % LED_FLASH_SET_PARAMETER_SUCCESS_COUNT) == 0) ? STRONG_PROMPTING_FLASHING_PERIOD : WEAK_CUE_FLASHING_PERIOD;
      break;

    case LED_HINT_KEY_OPERATION_SUCCESS:
      NextWakeupTime = (EventLedStatus.Count % LED_FLASH_KEY_OPERATION_SUCCESS_COUNT == 0) ? STRONG_PROMPTING_FLASHING_PERIOD : WEAK_CUE_FLASHING_PERIOD;
      break;

    case LED_HINT_DEVICE_INTO_STANDBY_MODE:
      NextWakeupTime = DEVICE_ONE_SECOND_PERIOD;
      break;

    case LED_HINT_TIME_ERROR:
      NextWakeupTime = STRONG_PROMPTING_FLASHING_PERIOD;
      break;

    case LED_HINT_UNLOCK: // 开锁蓝灯常亮6s后关闭
      NextWakeupTime = UNLOCK_KEEY_LIGNTING_PERIOD;
      break;

    case LED_HINT_SET_IO_BOARD_FIRMWARE_UPDATE_STATUS:
      NextWakeupTime = IO_BOARD_FIRMWARE_UPDATE_STATUS_PERIOD;
      break;

    case LED_HINT_DEVICE_INITIALIZING: // 事件灯初始化
      NextWakeupTime = DEVICE_ONE_SECOND_PERIOD;
      break;

    default:
      NextWakeupTime = WEAK_CUE_FLASHING_PERIOD;
      break;
    }

    SetEventLedPinLevel(true);

    BasicDelayedStartEventLedTask(NextWakeupTime);

    EventLedStatus.Status = LED_HINT_OFF;
    if (EventLedStatus.Count == LOOP_FOREVER_COUNT) // 特殊值
    {
      break;
    }

    if (EventLedStatus.Count > 0)
    {
      EventLedStatus.Count--;
    }
  }
  break;

  case LED_HINT_OFF:
  {

    SetEventLedPinLevel(false);

    if (EventLedStatus.Count > 0)
    {
      switch (EventLedStatus.Mode)
      {
      case LED_HINT_CONNECT_TEST:
        NextWakeupTime = ((AlarmLedStatus.Count % 5) == 0) ? STRONG_PROMPTING_FLASHING_PERIOD : MEDIUM_FLASHING_PERIOD;
        break;

      case LED_HINT_SET_PARAMETER_SUCCESS:
        NextWakeupTime = ((EventLedStatus.Count % 3) == 2) ? STRONG_PROMPTING_FLASHING_PERIOD : WEAK_CUE_FLASHING_PERIOD;
        break;

      case LED_HINT_DEVICE_INTO_STANDBY_MODE:
        NextWakeupTime = DEVICE_ONE_SECOND_PERIOD;
        break;

      case LED_HINT_SET_IO_BOARD_FIRMWARE_UPDATE_STATUS:
        NextWakeupTime = IO_BOARD_FIRMWARE_UPDATE_STATUS_PERIOD;
        break;

      case LED_HINT_DEVICE_INITIALIZING: // 初始化
        NextWakeupTime = DEVICE_ONE_SECOND_PERIOD;
        break;

      default:
        NextWakeupTime = MEDIUM_FLASHING_PERIOD;
        break;
      }

      EventLedStatus.Status = LED_HINT_FLASHES;
      BasicDelayedStartEventLedTask(NextWakeupTime);
    }
    else
    {
      CloseAlarmBreathingLight();
      EventLedStatus.Mode = LED_HINT_IDLE; // 空闲
    }
  }
  break;

  case LED_HINT_NORMAILON:
  {
    SetEventLedPinLevel(true);
  }
  break;

  case LED_HINT_BREATHING_MODE:
  {
    if (BreathingLight.HintStatus == false)
    {
      BreathingLight.DutyCycle += BREATHING_LIGHT_INCREASING_VALUE;
      if (BreathingLight.DutyCycle >= 100)
      {
        BreathingLight.HintStatus = true;
      }
    }
    else
    {
      BreathingLight.DutyCycle -= BREATHING_LIGHT_INCREASING_VALUE;
      if (BreathingLight.DutyCycle == 0)
      {
        BreathingLight.HintStatus = false;
      }
    }

    switch (AlarmLedStatus.Mode)
    {
    case LED_HINT_DEVICE_SELF_TEST:
      NextWakeupTime = (uint8_t)(BREATHING_LIGHT_FLASHING_PERIOD / 2);
      break;

    case LED_HINT_START_SAFETY_MODULE_AUTHENTICATE:
      NextWakeupTime = (uint8_t)(BREATHING_LIGHT_FLASHING_PERIOD / 5);
      break;

    default:
      NextWakeupTime = BREATHING_LIGHT_FLASHING_PERIOD;
      break;
    }

    UpdateAlarmBreathingLightDutyCycle(BreathingLight.DutyCycle);
    BasicDelayedStartEventLedTask(NextWakeupTime);
  }
  break;

  default:
    break;
  }
}

static bool CurrentStatusFlag = true;
void AlarmLedTaskHandler(void)
{
  uint16_t NextWakeupTime = 0;
  if (AlarmLedStatus.Count >= 0 && AlarmLedStatus.Mode != LED_HINT_IDLE)
  {
    // 记录当前蓝灯和绿灯的状态
    if (CurrentStatusFlag)
    {
      TempEventLedTaskStaus = EventLedStatus.Mode;

      TempResultLedTaskStaus = ResultLedStatus.Mode;
      PRINT("EventLedStatus.Mode = %d\n", EventLedStatus.Mode);
      PRINT("ResultLedStatus.Mode = %d\n", ResultLedStatus.Mode);

      // 已授权
      if (BasicGetDeviceActivationStatus() == true)
      {
        // 关闭其他灯，只执行红灯的状态
        SetResultLedPinLevel(false);
        // 关闭其他灯光任务
        BasicStopResultLedTask();
      }
      else // 未授权
      {
        // 关闭其他灯，只执行红灯的状态
        SetEventLedPinLevel(false);
        // 关闭其他灯光任务
        BasicStopEventLedTask();
      }

      CurrentStatusFlag = false;
    }
  }

  switch (AlarmLedStatus.Status)
  {
  case LED_HINT_FLASHES:
  {
    switch (AlarmLedStatus.Mode)
    {
    case LED_HINT_DECICE_SELF_TEST_ERROR:
      NextWakeupTime = (AlarmLedStatus.Count == 1) ? LONG_DELAY_TIME : WEAK_CUE_FLASHING_PERIOD;
      break;

    case LED_HINT_DEVICE_INTO_STANDBY_MODE:
      NextWakeupTime = WEAK_CUE_FLASHING_PERIOD;
      break;

    case LED_HINT_LOCKED_TIGGER:
    case LED_HINT_DEVICE_LOCKED_STATE:
      NextWakeupTime = ALARM_HINT_FLASHING_PERIOD;
      break;

    case LED_HINT_SYSTEM_LOCKED:
      NextWakeupTime = LONG_DELAY_TIME;
      break;

    default:
      NextWakeupTime = (AlarmLedStatus.Count == 1) ? STRONG_PROMPTING_FLASHING_PERIOD : WEAK_CUE_FLASHING_PERIOD;
      break;
    }
    //红灯亮之前关闭其他灯
    SetEventLedPinLevel(false);
    SetResultLedPinLevel(false);

    SetAlarmLedPinLevel(true);
    BasicDelayedStartAlarmLedTask(NextWakeupTime);

    AlarmLedStatus.Status = LED_HINT_OFF;
    if (AlarmLedStatus.Count > 0)
    {
      AlarmLedStatus.Count--;
    }
  }
  break;

  case LED_HINT_OFF:
  {
    SetAlarmLedPinLevel(false);

    if (AlarmLedStatus.Count > 0)
    {
      AlarmLedStatus.Status = LED_HINT_FLASHES;

      switch (AlarmLedStatus.Mode)
      {
      case LED_HINT_ANTI_BREAKING_ALARM_TIGGER:
        NextWakeupTime = STRONG_PROMPTING_FLASHING_PERIOD;
        break;

      case LED_HINT_CONNECT_TEST:

        NextWakeupTime = ((AlarmLedStatus.Count % 5) == 0) ? STRONG_PROMPTING_FLASHING_PERIOD : MEDIUM_FLASHING_PERIOD;
        break;

      case LED_HINT_DEVICE_INITIALIZING:
        NextWakeupTime = ((AlarmLedStatus.Count % 4) == 0) ? STRONG_PROMPTING_FLASHING_PERIOD : MEDIUM_FLASHING_PERIOD;
        break;

      case LED_HINT_FALSE_LOCK_TIGGER:
        NextWakeupTime = ((AlarmLedStatus.Count % 3) == 0) ? STRONG_PROMPTING_FLASHING_PERIOD : MEDIUM_FLASHING_PERIOD;
        break;

      case LED_HINT_LOW_POWER:
        NextWakeupTime = ((AlarmLedStatus.Count % 2) == 0) ? STRONG_PROMPTING_FLASHING_PERIOD : MEDIUM_FLASHING_PERIOD;
        break;

      default:
        NextWakeupTime = MEDIUM_FLASHING_PERIOD;
        break;
      }
      BasicDelayedStartAlarmLedTask(NextWakeupTime);
    }
    else
    {
      CloseAlarmBreathingLight();
      AlarmLedStatus.Mode = LED_HINT_IDLE; // 空闲

      // 恢复蓝灯和绿灯的状态
      if (CurrentStatusFlag == false)
      {
        CurrentStatusFlag = true;
        RestoreOtherLedStatus();
      }
    }
  }
  break;

  case LED_HINT_BREATHING_MODE:
  {
    if (BreathingLight.HintStatus == false)
    {
      BreathingLight.DutyCycle += BREATHING_LIGHT_INCREASING_VALUE;
      if (BreathingLight.DutyCycle >= 100)
      {
        BreathingLight.HintStatus = true;
      }
    }
    else
    {
      BreathingLight.DutyCycle -= BREATHING_LIGHT_INCREASING_VALUE;
      if (BreathingLight.DutyCycle == 0)
      {
        BreathingLight.HintStatus = false;
      }
    }

    switch (AlarmLedStatus.Mode)
    {
    case LED_HINT_DEVICE_SELF_TEST:
      NextWakeupTime = (uint8_t)(BREATHING_LIGHT_FLASHING_PERIOD / 2);
      break;

    case LED_HINT_START_SAFETY_MODULE_AUTHENTICATE:
      NextWakeupTime = (uint8_t)(BREATHING_LIGHT_FLASHING_PERIOD / 5);
      break;

    default:
      NextWakeupTime = BREATHING_LIGHT_FLASHING_PERIOD;
      break;
    }

    UpdateAlarmBreathingLightDutyCycle(BreathingLight.DutyCycle);
    BasicDelayedStartAlarmLedTask(NextWakeupTime);
  }
  break;

  default:
    break;
  }
}

void KeyboardLedTaskHandler(void)
{
  uint16_t NextWakeupTime = 0;

  switch (KeyboardLedStatus.Status)
  {
  case LED_HINT_FLASHES:
  {
    SetKeyboardLedPinLevel(true);
    BasicDelayedStartKeyboardLedTask(NextWakeupTime);

    KeyboardLedStatus.Status = LED_HINT_OFF;
    if (KeyboardLedStatus.Count > 0)
    {
      KeyboardLedStatus.Count--;
    }
  }
  break;

  case LED_HINT_OFF:
  {
    SetKeyboardLedPinLevel(false);

    if (KeyboardLedStatus.Count > 0)
    {
      switch (KeyboardLedStatus.Mode)
      {
      case LED_HINT_SET_PARAMETER_SUCCESS:
        NextWakeupTime = ((KeyboardLedStatus.Count % 3) == 2) ? STRONG_PROMPTING_FLASHING_PERIOD : WEAK_CUE_FLASHING_PERIOD;
        break;

      default:
        NextWakeupTime = MEDIUM_FLASHING_PERIOD;
        break;
      }

      KeyboardLedStatus.Status = LED_HINT_FLASHES;
      BasicDelayedStartKeyboardLedTask(NextWakeupTime);
    }
    else
    {
      KeyboardLedStatus.Mode = LED_HINT_IDLE;
    }
  }
  break;

  case LED_HINT_NORMAILON:
  {
    SetKeyboardLedPinLevel(true);
  }
  break;

  default:
    break;
  }
}

bool CheckIfTheIndicatorLightIsOn(void)
{

  if (AlarmLedStatus.Mode == LED_HINT_IDLE)
  {
    if (EventLedStatus.Mode == LED_HINT_IDLE)
    {

      if (ResultLedStatus.Mode == LED_HINT_IDLE)
        return false;
    }
  }

  return true;
}

// 结果灯处理
void ResultLedTaskHandler(void)
{
  uint16_t NextWakeupTime = 0;

  switch (ResultLedStatus.Status)
  {
  case LED_HINT_FLASHES:
  {
    switch (ResultLedStatus.Mode)
    {

    case LED_HINT_SET_COMPLETE_STATUS:
      NextWakeupTime = GREEN_LEDLONG_DELAY_TIME; // 常亮3秒
      break;

    case LED_HINT_DEVICE_INITIALIZING:                      // 初始化
      NextWakeupTime = DEVICE_UNAUTHORIZED_BLINKING_PERIOD; // 常亮4秒后关闭
      break;

    case LED_HINT_DEVICE_INTO_STANDBY_MODE:
      break;

    case LED_HINT_UNLOCK:                           // 开锁,特殊处理
      NextWakeupTime = UNLOCK_KEEY_LIGNTING_PERIOD; // 常灭6秒后开启
      SetResultLedPinLevel(false);
      BasicDelayedStartResultLedTask(NextWakeupTime);
      ResultLedStatus.Status = LED_HINT_NORMAILON;
      return;

    default:
      NextWakeupTime = (AlarmLedStatus.Count == 1) ? STRONG_PROMPTING_FLASHING_PERIOD : WEAK_CUE_FLASHING_PERIOD;
      break;
    }
    SetResultLedPinLevel(true); //
    BasicDelayedStartResultLedTask(NextWakeupTime);

    ResultLedStatus.Status = LED_HINT_OFF;
    if (ResultLedStatus.Count > 0)
    {
      ResultLedStatus.Count--;
    }
  }
  break;

  case LED_HINT_OFF:
  {
    SetResultLedPinLevel(false);

    if (ResultLedStatus.Count > 0)
    {
      switch (ResultLedStatus.Mode)
      {

      default:
        NextWakeupTime = MEDIUM_FLASHING_PERIOD;
        break;
      }

      ResultLedStatus.Status = LED_HINT_FLASHES;
      BasicDelayedStartResultLedTask(NextWakeupTime);
    }
    else
    {
      ResultLedStatus.Mode = LED_HINT_IDLE; // 空闲
    }
  }
  break;

  case LED_HINT_NORMAILON:
  {
    SetResultLedPinLevel(true);
  }
  break;

  default:
    break;
  }
}

void LedBoardInitialisation(void)
{
  AlarmLedPinConfiguration();
  EventLedPinConfiguration();
  ResultLedPinConfiguration();
  KeyboardLedPinConfiguration();
}

void CloseAllLed(void)
{
  SetAlarmLedPinLevel(false);
  SetEventLedPinLevel(false);
  SetResultLedPinLevel(false);
}
